package com.shop.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.Service.GoodService;
import com.shop.dao.UserMapper;
import com.shop.mapper.GoodMapper;
import com.shop.pojo.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Override
    public Message<String> addGood(Good good) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        int i =mapper.addGood(good);
        if (i>0){
            sqlSession.commit();
            message.setMessage("添加成功");
            message.setData("添加成功");
        }else {
            message.setMessage("添加失败");
        }
        return message;
    }

    @Override
    public Message<String> updateGood(Good good) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        int i = mapper.updateGood(good);
        if (i>0){
            sqlSession.commit();
            message.setMessage("修改成功");
            message.setData("修改成功");
        }else {
            message.setMessage("修改失败");
        }
        return message;
    }

    @Override
    public Message<String> deleteGood(int sid) throws Exception {
        Message<String> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        int i = mapper.deleteGood(sid);
        if (i>0){
            sqlSession.commit();
            message.setMessage("删除成功");
            message.setData("删除成功");
        }else {
            message.setMessage("删除失败");
        }
        return message;
    }

    @Override
    public Message<PageInfo<Goods>> selectGood(String queryInfo, int pageSize, int pageNum) throws Exception {
        Message<PageInfo<Goods>> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        PageHelper.startPage(pageNum,pageSize);
        if (queryInfo==null){
            queryInfo="";
        }
        List<Goods> goodsList =  mapper.selectGood(queryInfo);
        message.setMessage("获取成功");
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
        message.setData(pageInfo);
        return message;
    }
    @Override
    public Message<Integer> selectCount() throws Exception{
        Message<Integer> message = new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        int num = mapper.selectCount();
        message.setMessage("获取成功");
        message.setData(num);
        return message;
    }

    @Override
    public Message<Good> selectById(int sid) throws Exception {
        Message<Good> message =new Message<>();
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        GoodMapper mapper = sqlSession.getMapper(GoodMapper.class);
        Good good =new Good();
        good = mapper.selectById(sid);
        message.setData(good);
        message.setMessage("获取成功");
        return message;
    }
}
